import 'package:mi_mang_flutter/model/public/file_model.dart';
import 'package:mi_mang_flutter/model/public/oauth_model.dart';
import 'package:mi_mang_flutter/model/public/page_model.dart';
import 'package:mi_mang_flutter/model/user_info_model.dart';

import '../daily_word_model.dart';

///Model解析器(请求中需要解析的泛型需要在此处添加解析过程)
///该类是为了简化api请求中的操作
class EntityFactory {
  /// 将请求返回的数据解析成对象
  static T? generateOBJ<T>(json) {
    if (json == null) {
      return null;
    } else if (T.toString() == "OAuthModel") {
      return OAuthModel.fromJson(json) as T;
    } else if (T.toString() == "UserInfoModel") {
      return UserInfoModel.fromJson(json) as T;
    } else if (T.toString() == "FileModel") {
      return FileModel.fromJson(json) as T;
    } else {
      return null;
    }
  }

  /// 将请求返回的分页数据解析成对象
  static PageModel<T> generatePageOBJ<T>(json) {
    PageModel oldPageModel = PageModel.fromJson(json);
    List<T> list = [];
    if (T.toString() == "DailyWordModel") {
      oldPageModel.records!.forEach((v) {
        list.add(DailyWordModel.fromJson(v) as T);
      });
    }
    return PageModel<T>(
      records: list,
      total: oldPageModel.total,
      size: oldPageModel.size,
      current: oldPageModel.current,
      searchCount: oldPageModel.searchCount,
      pages: oldPageModel.pages,
    );
  }

  /// 将请求返回的列表数据解析成对象
  static List<T> generateListOBJ<T>(json) {
    List<T> records = [];
    return records;
  }
}
